<?php

namespace app\wechat\controller;

use app\common\model\UserInfoModel;
use Gaoming13\WechatPhpSdk\Api;
use think\Request;

/**
 * Class Index
 * @package app\wechat\controller
 */
class Index extends \think\Controller
{
    // 初始化微信接口
    public $api;
    // 开发者中心-配置项-AppID(应用ID)
    public $appId = 'wx76ae1f897a6d853e';
    // 开发者中心-配置项-AppSecret(应用密钥)
    public $appSecret = '733c47df8de27a41e47aae1997f1bf5f';

    /**
     * 初始化微信接口
     *
     * Index constructor.
     * @param Request|null $request
     */
    public function __construct(Request $request = null)
    {
        $this->api = new Api(array(
            'appId' => $this->appId,
            'appSecret' => $this->appSecret
        ));
        parent::__construct($request);
    }

    /**
     * 微信授权拿取
     *
     * @return \think\response\Json
     */
    public function index()
    {
        $userinfo_by_authorize = $this->api->get_wechat_info();
        return $this->ok($userinfo_by_authorize);
    }

    /**
     * 会员注册
     *
     * @return \think\response\Json
     */
    public function userReg()
    {
        $arr = $this->ioAll();
        $arr['times'] = 0;
        $userModel = new UserInfoModel();
        $userSave = $userModel->updateEx($arr);
        return $userSave ? $this->ok($userSave) : $this->fail("注册失败");
    }
}
